package com.demo.compass.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.compass.core.CompassHits;
import org.compass.core.CompassTemplate;

public class Buscador<K> 
{
	
	CompassTemplate compassTemplate = null;
	
	public void setCompassTemplate(CompassTemplate compassTemplate) {
		this.compassTemplate = compassTemplate;
	}
    @SuppressWarnings("unchecked")
	public Map<String, List<K>> search(String searchText){
		searchText= searchText+"*";
		CompassHits compassHits = (CompassHits) compassTemplate.find(searchText);
		Map<String, List<K>> matchedObjectMap = new HashMap<String, List<K>>();
		List<K> objectList = null;
		for (int i = 0; i < compassHits.length(); i++) {			
			K obj = (K) compassHits.data(i);
			String objectName = obj.getClass().getName();
			if(matchedObjectMap.containsKey(objectName)){
				objectList = matchedObjectMap.get(objectName);
				objectList.add(obj);
				matchedObjectMap.put(objectName, objectList);
			}else{
				objectList = new ArrayList<K>();
				objectList.add(obj);
				matchedObjectMap.put(objectName, objectList);
			}
		}
		//System.out.println("<<=============The matched Objects for the giver Search key==>"+ searchText+ " is ===>" + matchedObjectMap);
		return matchedObjectMap;	
	}

}

